//#preprocess
/**
 * AUTO_COPYRIGHT_SUB_TAG
 */
package com.rim.samples.device.push.ui;

import net.rim.device.api.i18n.DateFormat;
import net.rim.device.api.system.Application;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.NullField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;

import com.rim.samples.device.push.PushController;

/**
 * Main screen in the application. It contains most of the menu items.
 */
public class WelcomeScreen extends MainScreen implements FieldChangeListener {

    private static final String WELCOME_LABEL = "Welcome to the Push API sample";
    private static final String ACTION_LABEL = "Current action: ";

    private static final String MESSAGE_STATISTICS_LABEL = "Message Statistics";
    private static final String MESSAGE_COUNT_LABEL = "Message Count (unread/total): ";
    private static final String LAST_MESSAGE_LABEL = "Last message on: ";
    private static final String VIEW_MESSAGES_LABEL = "View Messages";

    private LabelField welcomeLabel;
    private LabelField actionLabel;

    private LabelField messageStatLabel;
    private LabelField messageCountLabel;
    private LabelField timeLabel;

    private ButtonField viewMessages;

    private CancelRequestItem cancelRequestItem;

    private static final int SEP_IDX1 = 0x00010000;
    private static final int SEP_IDX2 = 0x00020000;

    private DateFormat dateFormat;

    public WelcomeScreen() {
        setTitle( "Push API Sample" );

        welcomeLabel = new LabelField( WELCOME_LABEL, Field.FIELD_HCENTER );

        messageStatLabel = new LabelField( MESSAGE_STATISTICS_LABEL );
        messageCountLabel = new LabelField( MESSAGE_COUNT_LABEL + 0 + "/" + 0 );
        timeLabel = new LabelField( LAST_MESSAGE_LABEL + "-" );

        messageStatLabel.setFont( messageStatLabel.getFont().derive( Font.BOLD ) );

        viewMessages = new ButtonField( VIEW_MESSAGES_LABEL, ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY | FIELD_HCENTER );
        viewMessages.setChangeListener( this );

        dateFormat = DateFormat.getInstance( DateFormat.DATETIME_DEFAULT );

        add( new NullField( Field.FOCUSABLE ) );

        add( welcomeLabel );
        add( new SeparatorField() );

        add( messageStatLabel );
        add( messageCountLabel );
        add( timeLabel );
        add( new SeparatorField() );

        //#ifndef HANDHELD_VERSION_42
        add( viewMessages );
        //#endif

        addMenuItem( new ViewMessagesMenuItem() );
        addMenuItem( new RegisterMenuItem() );
        addMenuItem( new UnregisterMenuItem() );
        addMenuItem( new SuspendMenuItem() );
        addMenuItem( new ResumeMenuItem() );
        addMenuItem( new ViewLogItem() );
        addMenuItem( new ViewConfigItem() );
    }

    public void setAction( String action ) {
        if( action != null ) {
            if( actionLabel == null ) {
                actionLabel = new LabelField( ACTION_LABEL + action, Field.FIELD_HCENTER );
                insert( actionLabel, 2 );
            } else {
                actionLabel.setText( ACTION_LABEL + action );
            }
            if( cancelRequestItem == null ) {
                cancelRequestItem = new CancelRequestItem( action );
                addMenuItem( cancelRequestItem );
            } else {
                cancelRequestItem.setAction( action );
            }
        } else {
            if( actionLabel != null ) {
                delete( actionLabel );
                actionLabel = null;
            }
            if( cancelRequestItem != null ) {
                removeMenuItem( cancelRequestItem );
                cancelRequestItem = null;
            }
        }
    }

    public void setCount( int totalCount, int unreadCount, long lastMessageTimestamp ) {
        messageCountLabel.setText( MESSAGE_COUNT_LABEL + unreadCount + "/" + totalCount );
        if( lastMessageTimestamp > 0 ) {
            timeLabel.setText( LAST_MESSAGE_LABEL + dateFormat.formatLocal( lastMessageTimestamp ) );
        } else {
            timeLabel.setText( LAST_MESSAGE_LABEL + "-" );
        }
    }

    public boolean onClose() {
        // check whether we can close the application
        if( PushController.canQuit() ) {
            return super.onClose();
        } else {
            Application.getApplication().requestBackground();
            return false;
        }
    }

    private static class ViewMessagesMenuItem extends MenuItem {

        public ViewMessagesMenuItem() {
            super( "View Messages", 0, 1 );
        }

        public void run() {
           // PushController.showMessages();
        }
    }

    private static class RegisterMenuItem extends MenuItem {

        public RegisterMenuItem() {
            super( "Register", SEP_IDX1 + 1, 1 );
        }

        public void run() {
            PushController.register();
        }
    }

    private static class UnregisterMenuItem extends MenuItem {

        public UnregisterMenuItem() {
            super( "Un-Register", SEP_IDX1 + 2, 1 );
        }

        public void run() {
            PushController.unregister();
        }
    }

    private static class SuspendMenuItem extends MenuItem {

        public SuspendMenuItem() {
            super( "Suspend", SEP_IDX1 + 3, 1 );
        }

        public void run() {
            PushController.suspend();
        }
    }

    private static class ResumeMenuItem extends MenuItem {

        public ResumeMenuItem() {
            super( "Resume", SEP_IDX1 + 4, 1 );
        }

        public void run() {
            PushController.resume();
        }
    }

    private static class ViewLogItem extends MenuItem {

        public ViewLogItem() {
            super( "View Log", SEP_IDX2 + 5, 1 );
        }

        public void run() {
            PushController.showLog();
        }
    }

    private static class ViewConfigItem extends MenuItem {

        public ViewConfigItem() {
            super( "Settings", SEP_IDX2 + 6, 1 );
        }

        public void run() {
            PushController.showConfigScreen();
        }
    }

    private static class CancelRequestItem extends MenuItem {

        public CancelRequestItem( String action ) {
            super( "Cancel request to " + action, SEP_IDX2 + 7, 1 );
        }

        public void run() {
            PushController.cancelCurrentCommand();
        }

        public void setAction( String action ) {
            setText( "Cancel request to " + action );
        }
    }

    public void fieldChanged( Field field, int context ) {
        if( field == viewMessages ) {
            //PushController.showMessages();
        }
    }
}
